package com.atguigu.wordcount;

/*
* WordCount - 有界流处理 -DataStream
*
* 步骤：
*  1.创建环境
*  2.从数据源读取数据
*  3.对读取的数据进行转换处理
*  4.写出结果
*  5.启动执行
*
*
* */

import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.java.functions.KeySelector;
import org.apache.flink.api.java.tuple.Tuple;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.util.Collector;

public class Flink02_BoundedStreamWordCount {
    public static void main(String[] args) {
        //创建环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        //设置并行度，设置为1意味着所有任务将顺序执行，不会并行处理。
        env.setParallelism(1);

        //2.从数据源中读取数据
        DataStreamSource<String> ds = env.readTextFile("F:\\代码\\11_12flink\\FlinkTutorial\\input\\word.txt");

        //3.将读取到的数据进行转换处理
        //3.1转换成tuple
        SingleOutputStreamOperator<Tuple2<String, Long>> dsflatMap = ds.flatMap(
                new FlatMapFunction<String, Tuple2<String, Long>>() {
                    @Override
                    public void flatMap(String line, Collector<Tuple2<String, Long>> out) throws Exception {
                        String[] words = line.split(" ");
                        for (String word : words) {
                            out.collect(Tuple2.of(word,1L));
                        }
                    }
                }
        );

        //3.2按照单词进行分组
        //获取到key
        KeyedStream<Tuple2<String, Long>, String> keyBy = dsflatMap.keyBy(
                new KeySelector<Tuple2<String, Long>, String>() {
                    @Override
                    public String getKey(Tuple2<String, Long> value) throws Exception {
                        return value.f0;
                    }
                }
        );
        //3.3统计每个单词出现的次数
        SingleOutputStreamOperator<Tuple2<String, Long>> sumDs = keyBy.sum(1);

        //4.输出结果
        sumDs.print();

        //5.启动执行
        try {
            env.execute();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }
}
